/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cl.innovision.helper.utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;

/**
 *
 * @author PATO
 */
public class Types {
    
    
    private static Types singleton = null;
    private static Properties datatypes = new Properties();
        private String baseDatos;
    private String tecnologia;
    FileInputStream input=null;
    
    public static Types getInstance( String baseDatos, String tecnologia) {
        if (singleton == null) {
            singleton = new Types(  baseDatos,  tecnologia);
        }
        return singleton;
    }/*
     public static Types getInstance( String baseDatos, String tecnologia) {
        if (singleton == null) {
            singleton = new Types(  baseDatos,  tecnologia);
        }
        return singleton;
    }
    */
    
    private static HashMap dbTypes =new HashMap();
    public Types( String baseDatos, String tecnologia) {
        this.baseDatos=baseDatos;
        this.tecnologia=tecnologia;
          try {
			input = new FileInputStream("template/"+baseDatos+"/"+tecnologia+"/datatypes.skl");
			datatypes.load(input);
			input.close();
                        loadBasicTypes();
		} catch (FileNotFoundException e) {
			System.out.println("No pudo encontrar archivo de propiedades :"+e.getMessage());
			System.exit(1);
		} catch (IOException e) {
			System.out.println("No pudo abrir archivo de propiedades :"+e.getMessage());
			System.exit(1);
		}
          
          
    }
    
    public void loadBasicTypes()
    {
        dbTypes.put("VARCHAR", "VARCHAR");
        dbTypes.put("CHAR", "VARCHAR");
        dbTypes.put("DATE", "DATE");
        dbTypes.put("INT", "INT");
        dbTypes.put("INTEGER", "INTEGER");    
        dbTypes.put("DATETIME", "DATETIME");
        dbTypes.put("TINYINT", "INT");
        dbTypes.put("SMALLINT", "INT");
        dbTypes.put("MEDIUMINT", "INT");
        dbTypes.put("BIGINT", "NUMERIC");
        dbTypes.put("FLOAT", "NUMERIC");
        dbTypes.put("DOUBLE", "NUMERIC");
        dbTypes.put("BLOB", "BLOB");
        dbTypes.put("DECIMAL", "DECIMAL");
        dbTypes.put("NUMERIC", "NUMERIC");
        dbTypes.put("REAL", "REAL");
        dbTypes.put("TEXT", "DATETIME");
        
        
        
        
    }
    
    
  
    public static String db2class(String type,int precision,short scale) {
		
        //System.out.println("Tipo SQL:"+type);
		String javaTypes = datatypes.getProperty(type,type);
		String[] javaType = javaTypes.split(";");
		for (int i = 0; i < javaType.length; i++) {
			String[] javaTypeParts = javaType[i].split(",");
			if (javaTypeParts.length == 6 && Integer.valueOf(javaTypeParts[0]) <= precision && Integer.valueOf(javaTypeParts[1]) >= precision && Integer.valueOf(javaTypeParts[2]) <= scale && Integer.valueOf(javaTypeParts[3]) >= scale) {
				//System.out.println("Tipo Java: "+javaTypeParts[4]);
                            return  javaTypeParts[4];
				//shortType = javaTypeParts[5];
				//break;
			}
		}
		//Attribute attribute = new Attribute(name, type, shortType, parameter.getDataTypeName());
		return null;
	} 
    
    public static String basicTypesDB(String typeBD)
    {
        return (String) dbTypes.get(typeBD);
    }
}
